package com.sinosoft.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sinosoft.common.Const;
import com.sinosoft.entity.DefUser;
import com.sinosoft.model.EasyUITree;
import com.sinosoft.service.IHomeService;

@Controller
public class HomeController {

	@Autowired
	private IHomeService homeService;
	
	/**
	 * 跳转到home.jsp页面
	 * @param modelAndView
	 * @return
	 */
	@RequestMapping("/home")
	public ModelAndView home(ModelAndView modelAndView) {
		modelAndView.setViewName("home");
		return modelAndView;
	}
	
	/**
	 * 初始化home.jsp页面上的一级导航菜单
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/home/initGuideMenu")
	public DefUser initGuideMenu(HttpServletRequest request) {
		return (DefUser) request.getSession().getAttribute(Const.SESSION_USER_KEY);
	}
	
	/**
	 * 初始化home.jsp页面上的二级导航菜单
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/home/initGuideMenu2")
	public List<EasyUITree> initGuideMenu2(Long pid, HttpServletRequest request) {
		return homeService.queryPrivilegeByPId(pid, request);
	}
	
	/**
	 * 登录
	 * @param user
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/login")
	public Map<String, Object> login(DefUser user, HttpServletRequest request) {
		return homeService.login(user, request);
	}
	
	/**
	 * 注销
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/logout")
	public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
		HttpSession session = request.getSession(false);
		if (null != session)
			session.invalidate();
		response.sendRedirect(request.getContextPath()+"/index.jsp");
	}
}
